package com.sxj.corejava.code10_异常;

import java.util.Scanner;

/**
 * @author 石小俊
 * @date 2025年02月20日
 */
public class Test08_程序调试 {
    public static void main(String[] args) {
        UserController userController = new UserController();
        userController.register();
    }
}

class UserDao {
    public void insertUser(String username, String password) {
        System.out.println("注册成功");
    }
}

class UserService {
    public void register(String username, String password) throws UserException {
        // 用户名不能重复,假设已经存在用户名admin了
        if ("admin".equals(username)) {
            throw new UserException("该用户名已经被注册");
        }
        UserDao userDao = new UserDao();
        userDao.insertUser(username, password);
    }
}

class UserController {
    public void register() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入用户名:");
        String username = sc.next();
        System.out.print("请输入密码:");
        String password = sc.next();
        UserService userService = new UserService();
        try {
            userService.register(username, password);
            for (int i = 0; i < 4; i++) {
                System.out.println(i);
            }
        } catch (UserException e) {
            System.out.println("注册异常,异常为:" + e.getMessage());
        }
    }

}